const exApp = require('../index.js')
const express = require('express');
const router = express.Router();
const mysql = require("../connect/pomsql")
const { SuccessModule, ErrorModule } = require('../model/resModel')
const { resHandler, resHandler1 } = require('../model/resHandler')
//会员管理模块
// 会员列表请求数据
router.get('/members', async (req, res, next) => {
    let sql = `select * from membersList`;
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//会员列表分页
router.get('/members/pages', async (req, res, next) => {
    let {size=5,page=1}=req.query
    let sql = `select * from membersList limit ${(page-1)*size},${size}`;
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//会员等级请求数据
router.get('/membersLevel', async (req, res, next) => {
    let sql = 'select * from membersLevel'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
// 会员等级添加
router.post('/addLevel', async (req, res) => {
    const {level_name,conditions,discount,state}=req.body
    let sql = `insert into membersLevel(level_name,conditions,discount,state) values ('${level_name}', '${conditions}', '${discount}', '${state}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from membersLevel;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from membersLevel where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
// 会员等级修改
router.post('/editLevel', async function (req, res) {
    let {level_name,conditions,discount,state,id}=req.body
    id=id*1
    let sql = `update membersLevel set level_name='${level_name}',conditions='${conditions}',discount='${discount}',state='${state}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//分组设置请求数据
router.get('/grouping', async (req, res, next) => {
    let sql = 'select * from `grouping`';
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//添加分组
router.post('/addGroup', async (req, res) => {
    const {class_name,describe_name}=req.body
    let sql = `insert into grouping(class_name, describe_name) values ('${class_name}', '${describe_name}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from grouping;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from grouping where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//删除分组一条数据
router.get('/delGroup', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from grouping where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
//活动管理模块
// 会场请求数据
router.get('/activityVenue', async (req, res, next) => {
    let sql = 'select * from activityVenue'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//添加会场
router.post('/addVenue', async (req, res) => {
    const {venue_name,start_time,create_time,cancel_time,state}=req.body
    let sql = `insert into activityVenue(venue_name,start_time,create_time,cancel_time,state) values ('${venue_name}', '${start_time}', '${create_time}','${cancel_time}', '${state}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from activityVenue;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from activityVenue where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//修改会场
router.post('/editVenue', async function (req, res) {
    let {venue_name,start_time,cancel_time,state,id}=req.body
    id=id*1
    let sql = `update activityVenue set venue_name='${venue_name}',start_time='${start_time}',cancel_time='${cancel_time}',state='${state}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 商品管理
router.get('/activitiesGoods', async (req, res, next) => {
    let sql = 'select * from activitiesGoods'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//秒杀管理添加商品
router.post('/spell/addGoods', async (req, res) => {
    const {sorting,belongs_to,classification,original_price,inventory,a_details,a_type}=req.body
    let sql = `insert into activitiesGoods(sorting,belongs_to,classification,original_price,inventory,a_details,a_type) values ('${sorting}', '${belongs_to}', '${classification}','${original_price}', '${inventory}', '${a_details}', '${a_type}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from activitiesGoods;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from activitiesGoods where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//秒杀修改商品
router.post('/spell/editGoods', async function (req, res) {
    let {sorting,belongs_to,classification,original_price,inventory,a_details,id}=req.body
    id=id*1
    let sql = `update activitiesGoods set sorting='${sorting}',belongs_to='${belongs_to}',classification='${classification}',original_price='${original_price}',inventory='${inventory}',a_details='${a_details}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除秒杀商品
router.get('/spell/delGoods', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from activitiesGoods where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
// 分类管理
router.get('/sortManage', async (req, res, next) => {
    let sql = 'select * from sortManage'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//秒杀管理添加商品分类
router.post('/spell/addClass', async (req, res) => {
    const {sequence,name,state,type}=req.body
    let sql = `insert into sortManage(sequence,name,state,type) values ('${sequence}', '${name}', '${state}','${type}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from sortManage;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from sortManage where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//秒杀修改分类
router.post('/spell/editClass', async function (req, res) {
    let {sequence,name,state,id}=req.body
    id=id*1
    let sql = `update sortManage set sequence='${sequence}',name='${name}',state='${state}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除秒杀分类
router.get('/spell/delClass', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from sortManage where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
// 轮播管理
router.get('/lbManage', async (req, res, next) => {
    let sql = 'select * from lbManage'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//秒杀管理添加轮播分类
router.post('/spell/addLb', async (req, res) => {
    const {sequence,name,state,type,link}=req.body
    let sql = `insert into lbManage(sequence,name,state,type,link) values ('${sequence}', '${name}', '${state}','${type}','${link}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from lbManage;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from lbManage where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//修改轮播分类
router.post('/spell/editLb', async function (req, res) {
    let {sequence,name,state,link,id}=req.body
    id=id*1
    let sql = `update lbManage set sequence='${sequence}',name='${name}',state='${state}',link='${link}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除轮播分类
router.get('/spell/delLb', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from lbManage where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
//拼团管理添加商品
router.post('/seconds/addGoods', async (req, res) => {
    const {sorting,classification,original_price,group_price,inventory,a_details,a_type,state}=req.body
    let sql = `insert into activitiesGoods(sorting,classification,original_price,group_price,inventory,a_details,a_type,state) values ('${sorting}', '${classification}','${original_price}','${group_price}', '${inventory}', '${a_details}', '${a_type}','${state}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from activitiesGoods;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from activitiesGoods where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//拼团修改商品
router.post('/seconds/editGoods', async function (req, res) {
    let {sorting,classification,original_price,group_price,inventory,a_details,state,id}=req.body
    id=id*1
    let sql = `update activitiesGoods set sorting='${sorting}',classification='${classification}',original_price='${original_price}',group_price='${group_price}',inventory='${inventory}',a_details='${a_details}',state='${state}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除拼团商品
router.get('/seconds/delGoods', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from activitiesGoods where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
//拼团管理添加商品分类
router.post('/seconds/addClass', async (req, res) => {
    const {sequence,name,state,type}=req.body
    let sql = `insert into sortManage(sequence,name,state,type) values ('${sequence}', '${name}', '${state}','${type}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from sortManage;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from sortManage where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//拼团修改分类
router.post('/seconds/editClass', async function (req, res) {
    let {sequence,name,state,id}=req.body
    id=id*1
    let sql = `update sortManage set sequence='${sequence}',name='${name}',state='${state}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//拼团秒杀分类
router.get('/seconds/delClass', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from sortManage where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
//秒杀管理添加轮播分类
router.post('/seconds/addLb', async (req, res) => {
    const {sequence,name,state,type,link}=req.body
    let sql = `insert into lbManage(sequence,name,state,type,link) values ('${sequence}', '${name}', '${state}','${type}','${link}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from lbManage;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from lbManage where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//修改轮播分类
router.post('/seconds/editLb', async function (req, res) {
    let {sequence,name,state,link,id}=req.body
    id=id*1
    let sql = `update lbManage set sequence='${sequence}',name='${name}',state='${state}',link='${link}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除轮播分类
router.get('/seconds/delLb', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from lbManage where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
// 活动状态管理
router.get('/activityManage', async (req, res, next) => {
    let sql = 'select * from activityManage'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//积分商城
// 添加商品
router.get('/codeGoods', async (req, res, next) => {
    let sql = 'select * from codeGoods'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
// 积分商城状态管理
router.get('/codeManage', async (req, res, next) => {
    let sql = 'select * from codeManage'
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
//秒杀管理添加轮播分类
router.post('/point/addLb', async (req, res) => {
    const {sequence,name,state,type,link}=req.body
    let sql = `insert into lbManage(sequence,name,state,type,link) values ('${sequence}', '${name}', '${state}','${type}','${link}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from lbManage;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from lbManage where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//修改轮播分类
router.post('/point/editLb', async function (req, res) {
    let {sequence,name,state,link,id}=req.body
    id=id*1
    let sql = `update lbManage set sequence='${sequence}',name='${name}',state='${state}',link='${link}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除轮播分类
router.get('/point/delLb', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from lbManage where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
//积分添加商品分类
router.post('/point/addClass', async (req, res) => {
    const {sequence,name,state,type}=req.body
    let sql = `insert into sortManage(sequence,name,state,type) values ('${sequence}', '${name}', '${state}','${type}')`
    let data = await mysql(sql)
    if(!data){
        res.json({
            code:-1,
        })
    }
    // 拿到最新的id值
    let sql2 =`select max(id) from sortManage;`
    let data2 = await mysql(sql2)
    if(!data2){
        res.json({
            code:-1,
        })
    }
    let id = data2[0]['max(id)']
    // 用id值查询数据
    let sql3 =`select * from sortManage where id =${id};`
    let data3 = await mysql(sql3)
    resHandler(res,data3,'操作失败')
})
//秒杀修改分类
router.post('/point/editClass', async function (req, res) {
    let {sequence,name,state,id}=req.body
    id=id*1
    let sql = `update sortManage set sequence='${sequence}',name='${name}',state='${state}'  where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
//删除秒杀分类
router.get('/point/delClass', async (req, res) => {
    let { id } = req.query;
    let sql = `delete from sortManage where id = ${id};`
    resHandler1(res, sql, '获取失败')
})
exApp.use('/', router);